package com.rabbiter.library.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rabbiter.library.annotation.Auth;
import com.rabbiter.library.dto.ExceptionParm;
import com.rabbiter.library.utils.ResultUtils;
import com.rabbiter.library.utils.ResultVo;
import com.rabbiter.library.dto.BookVo;
import com.rabbiter.library.entity.SysBooks;
import com.rabbiter.library.service.SysBooksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "图书管理接口")
@RestController
@RequestMapping("/api/books")
public class SysBooksController {
    @Autowired
    private SysBooksService sysBooksService;

    @ApiOperation(value = "添加图书", notes = "添加新的图书信息")
    @Auth
    @PostMapping
    public ResultVo add(@RequestBody SysBooks books){
        boolean save = sysBooksService.save(books);
        if(save){
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error("新增失败");
    }

    @ApiOperation(value = "编辑图书", notes = "根据ID更新图书信息")
    @Auth
    @PutMapping
    public ResultVo edit(@RequestBody SysBooks books){
        boolean save = sysBooksService.updateById(books);
        if(save){
            return ResultUtils.success("编辑成功");
        }
        return ResultUtils.error("编辑失败");
    }

    @ApiOperation(value = "删除图书", notes = "根据ID删除图书")
    @ApiImplicitParam(name = "bookId", value = "图书ID", required = true, dataTypeClass = Long.class, paramType = "path")
    @Auth
    @DeleteMapping("/{bookId}")
    public ResultVo delete(@PathVariable("bookId") Long bookId){
        boolean remove = sysBooksService.removeById(bookId);
         if(remove){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败");
    }

    @ApiOperation(value = "获取图书列表", notes = "分页查询图书信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "bookName", value = "图书名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "categoryId", value = "分类ID", dataTypeClass = Long.class)
    })
    @Auth
    @GetMapping("/list")
    public ResultVo getList(ExceptionParm.ListParm parm){
        IPage<SysBooks> list = sysBooksService.getList(parm);
        return ResultUtils.success("查询成功",list);
    }

    @ApiOperation(value = "获取热门图书", notes = "获取系统中的热门图书")
    @Auth
    @GetMapping("/getHotBook")
    public ResultVo getHotBook(){
        List<BookVo> hotBook = sysBooksService.getHotBook();
        return ResultUtils.success("查询成功",hotBook);
    }
}
